package cc.fs.zhihuapp.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.utils.RecyclerViewFooter;

/**
 * Created by fostion on 1/4/16.
 */
public class RefashAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements RecyclerViewFooter.AdapterFooterManager{

    private final int TYPE_ITEM = 0;
    private final int TYPE_FOOTER = 1;

    private List<String> list;
    private boolean isLoadingMore = true;

    public RefashAdapter(List<String> list){
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_ITEM){
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_normal,parent,false);
            return new ItemViewHolder(v);

        }

        if(viewType == TYPE_FOOTER){
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer,parent,false);
            return new FooterViewHolder(v);
        }
        throw new UnsupportedClassVersionError();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemViewType(int position) {
        if(isLoadingMore && position == getItemCount() - 1){
            return TYPE_FOOTER;
        }
        return TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        if(isLoadingMore)
            return list.size()+1;
        return list.size();
    }

    @Override
    public void setLoadingMore(boolean loadingMore) {
        isLoadingMore = loadingMore;
        notifyDataSetChanged();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder{
        public ItemViewHolder(View itemView) {
            super(itemView);
        }
    }

    static class FooterViewHolder extends RecyclerView.ViewHolder{
        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }
}
